package com.ceba.test.modules.usermanage.logicService;

import java.util.List;
import java.util.Map;

import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.usermanage.enums.UserRoleEnum;
import org.json.JSONArray;
import org.json.JSONObject;

import com.ceba.test.modules.usermanage.entity.UUser;
import com.ceba.test.modules.usermanage.service.LoginService;
import com.ceba.test.modules.usermanage.service.UserService;

public class UserManageLogicService {
	
	private UserService userService;
	private LoginService loginService;
	
	
	public String  getUserRoleByUserId(String userId){
		return userService.getUserRoleByUserId(userId);
	}
	
	
	public String getOrganizeUserListSerivce (Map<String,Object> dic) throws Exception{
	 return  userService.getOrganizeUserListSerivce(dic).toJSON();
	 
	}
	
	public String getUserList (Map<String,Object> params,String orderBy) throws Exception{
		String jsonString =  userService.getUserList(params,orderBy).toJSON();
		return getUserList(jsonString);
	}

	public String getUserListBySou (String key,String orderBy) throws Exception{
		String jsonString =  userService.getUserListBySou(key,orderBy).toJSON();
		 return getUserList(jsonString);
	}

	public String getUserList(String jsonString){
		JSONObject o = new JSONObject(jsonString);
		JSONArray arr = new JSONArray();
		if (o.has("rows")) {
			arr = o.getJSONArray("rows");
			if (arr.length() > 0) {
				for (int i = 0; i < arr.length(); i++) {
					JSONObject jo = arr.getJSONObject(i);
					if (jo.has("ROLE_ID")) {
						String id = jo.getString("ROLE_ID");
						String name = getRoleIdToNameI18(id);
						jo.put("ROLE_NAME",name);
					}
				}
				o.put("total",userService.queryUserCount());
			}
		}
		return o.toString();
	}

	public String getUserService (Map<String,Object>  dic) throws Exception{
		return userService.getUser(dic);
	}
	
	public String editOrganizeListService(Map<String,Object> dic) throws Exception{
		 return  userService.editOrganizeList(dic).toString();
	}
	
	public  String getOrganizeListService(Map<String,Object> dic) throws Exception{
		return  userService.getOrganizeList(dic).toJSON();
	}
	
	
	public  String editRoleService(Map<String,Object>  dic) throws Exception{
		return  userService.editRole(dic).toJSONArray().toString();
	}
	
	public  String getRoleService() throws Exception{
		String jsonString = userService.getRole().toJSON();
		JSONObject o = new JSONObject(jsonString);
		JSONArray arr = new JSONArray();
		if (o.has("rows")) {
			arr = o.getJSONArray("rows");
			if (arr.length() > 0) {
				for (int i = 0; i < arr.length(); i++) {
					JSONObject jo = arr.getJSONObject(i);
					if (jo.has("ID")) {
						String id = jo.getString("ID");
						String name = getRoleIdToNameI18(id);
						jo.put("NAME",name);
					}
				}
			}
		}
		//return userService.getRole(dic).toJSON();
		return o.toString();
	}

	
	public boolean addOrganize(Map<String,Object>  dic) throws Exception{
		return  userService.addOrganize(dic);
	}
	
	/**
	 * 编辑用户
	 * @param
	 * @param
	 * @return
	 */
	public  boolean editUserService(JSONObject obj,String loginUserId) throws Exception{
	  return userService.editUser(obj,loginUserId);
	}


	public boolean addUserService(Map<String,Object> dic,String loginUserId,String id,String action) throws Exception{
		return userService.addUser(dic,loginUserId,id,action);
	}

	public boolean getUserByLoginId(String loginUserId){
		return userService.getUserByLoginId(loginUserId);
	}

	public boolean addAdDomainUserService(Map<String,Object> dic,String loginUserId,String id,String action) throws Exception{
		return userService.addAdDomainUser(dic,loginUserId,id,action);
	}

	public boolean getUserQueryByName(String loginId){
		return userService.getUserQueryByName(loginId);
	}

	public boolean getUserQueryByNameAndId(String loginId,String id){
		return userService.getUserQueryByNameAndId(loginId,id);
	}

	public boolean deleteUser(String ID) throws Exception{
		return userService.deleteUser(ID);
	}
	
	public  String getUserRole(String userId ) throws Exception{
		return userService.getUserRole(userId);
	}
	
	/**
	 * 获取所有的用户
	 * @return
	 * @throws Exception
	 */
	public List<UUser> getUserIds() throws Exception{
		return userService.getUserIds();
	}
	
	/**
	 * 获取未添加用户章的用户
	 * @return
	 * @throws Exception
	 */
	public List<UUser> getUserIdsNotInStamp() throws Exception{
	
		return userService.getUserIdsNotInStamp();
	}
	//LoginService 类
	
    public UUser login(String loginID) throws Exception{
    	return loginService.login(loginID);
    }

    public UUser adLogin(String loginID) throws Exception{
    	return loginService.adLogin(loginID);
    }
    /**
  	 * 保存密码
  	 * @param pw
  	 * @param userId
  	 * @return
  	 */
  	public Boolean savePassword(String pw,String userId){
  		return loginService.savePassword(pw, userId);
  	}
  	
  	/**
	 * 获取当前用户的管辖权限
	 * @param userId 用户ID
	 * @param roleId 用户角色
	 * @return 返回一个数组：[0]代表组织id集合，[1]代表用户id集合
	 */
	public String[]  getUserPrivilege(String userId,String roleId){
		return loginService.getUserPrivilege(userId, roleId);
	}

	public Map<String,Object> queryUserById(String userId){
		return userService.queryUserById(userId);
	}
	
	/**
	 * 根据ID获取用户
	 */
	public UUser getUserById(String userId){
		return userService.getUserById(userId);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public LoginService getLoginService() {
		return loginService;
	}


	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public String getRoleIdToNameI18(String id){
		if(id.equals(UserRoleEnum.DIGITAL_ENCRYPT.getCode())){
			return ResourceUtils.getMessage("ADMIN_DIGITAL_ENCRYPT");
		}else if(id.equals(UserRoleEnum.VERIFYING_PLATFORM_ADMINISTATOR.getCode())){
			return ResourceUtils.getMessage("ADMIN_VERIFYING_PLATFORM_ADMINISTRATOR");
		}
		return null;
	}

}
